//
//  NSURL+UrlEnocde.m
//  IPlayMTG
//
//  Created by 胖虎 on 2021/4/21.
//  Copyright © 2021 iplaymtg. All rights reserved.
//

#import "NSURL+UrlEnocde.h"
#import <objc/runtime.h>

@implementation NSURL (UrlEnocde)

//加载类的load方法

+ (void)load
{
    method_exchangeImplementations(class_getClassMethod([NSURL class], @selector(URLWithString:)), class_getClassMethod([NSURL class], @selector(XL_URLWithString:)));
}

+ (instancetype)XL_URLWithString:(NSString *)URLString{
    //NSURL *url = [NSURL URLWithString:URLString];
    //上边这一句会出现死循环，因为交换机制调用URLWithString执行XD_URLWithString那么
    //直接调用XD_URLWithString，因为刚才通过了交换，就相当于调用URLWithString，就像大话西游上移神换影大法😄
    //NSLog(@"bdurl=%@", URLString);
    NSLog(@"12312312---12312");
    NSURL *url = [NSURL XL_URLWithString:URLString];
    if (url == nil && URLString) {
        NSString *version = [UIDevice currentDevice].systemVersion;
        if (version.doubleValue >= 17.0) {
            url = [NSURL URLWithString:URLString];
        } else {
            NSString * urlstr = [URLString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
            url = [NSURL URLWithString:urlstr];
        }
        
    }
    return url;
}

@end
